package DControl;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DTextField extends DComponent implements Runnable {
	 int index = 0;
	 int length = 0;
	 String curString = "";
	 DCursor cursor;
	 ArrayList<Integer> size = new ArrayList<Integer>();
	 ArrayList<Integer> locate = new ArrayList<Integer>();
	 boolean setCursor = false;
	 boolean input = false;
	 Thread thread;
	 public DTextField()
	 {
		 init();
		 setResponseRect(x, y, x + width, y + height);
	 }
	 
	 public DTextField(int length)
	 {
		 init();
		 this.width = length*8;
		 this.length = length;
		 setResponseRect(x, y, x + width, y + height);
	 }
	 
	 public DTextField(int x,int y,int length)
	 {
		 init();
		 this.x = x;
		 this.y = y;
		 this.width = length*8;
		 this.length = length;
		 setResponseRect(x, y, x + width, y + height);
	 }
	 
	 public void init()
	 {
		 x = 0;
		 y = 0;
		 width = 80;
		 height = 20;
		 text = "";
		 color = Color.white;
		 Class = "DTextField";

		 thread = new Thread(this);
		 thread.start();
		 
		
	 }
	 
	 public void run()
		{
		    while(true)
		    {
		    	try
				{
					Thread.sleep(500);
				}catch(Exception e)
				{
					e.printStackTrace();
				}
					
			    //tfd.repaint();
			    repaint();
			    //System.out.println(index);
				
		    }
		
		}
	 
	 public void paintComponent(Graphics g)
	 {
		 FontMetrics metrics = g.getFontMetrics();
		 
		 g.setColor(color);
		 g.fill3DRect(x, y, width, height, false);
		 
		 if(setCursor)
		 {
		   g.setColor(Color.black);
		   g.drawString(text,x,y + 14);
		   if(input)
		   {   int metric = metrics.stringWidth(curString);
			   cursor.moveNext(metric); 
			  
			   for(int i = 0; i < curString.length(); i++)
			   {
				   int tempMetric = metrics.stringWidth(curString.substring(i, i + 1));
				   System.out.println("temp : " + tempMetric);
				   
				   size.add(tempMetric);
				   if(index == 0)
				   {
					   locate.add(tempMetric);
				   }
				   else
				   {
					   locate.add(locate.get(index - 1) + tempMetric);
				   }
			   }
			  
			   input = false;
			   index += curString.length();
			   curString = "";

		   }
		   if(focus)
		   {
			   cursor.paintComponent(g);
		   }
		 }
		 
	 }
	 
	
	 public void setLength(int length)
	 {
		 this.width = length*10;
	 }
	 
	  public void mouseEntered(MouseEvent e){}
   	 
	  public void mouseExited(MouseEvent e){}
	   	 
	  public void mousePressed(MouseEvent e)
	  {
		  if(!setCursor)
		  {
			  cursor = new DCursor(x,y,this);
			  setCursor = true;
		  }
		  else
		  {
			
			  int temp = 0 ;
			  int tempM = e.getX() - x;
			  for(int i = 1; i < locate.size(); i ++)
			  {
				  if(Math.abs(locate.get(i)-tempM) < Math.abs(locate.get(temp)-tempM))
				  {
					  temp = i;
				  }
			  }
			  cursor.setLocation(x + locate.get(temp), y);
		  }
		 System.out.println("DTextField");
	  }
	   	 
	  public void mouseReleased(MouseEvent e){}
	   	 
	  public void mouseClicked(MouseEvent e){}
	  
	  public void keyTyped(KeyEvent e)
	  {
		    curString += e.getKeyChar();
			text += e.getKeyChar();
			input = true;
	  }
	  
	  public void keyPressed(KeyEvent e)
		{
			int key = e.getKeyCode();
			if(focus)
			{
				/*if(key >= 48 && key <= 105)
				{
					curString += e.getKeyChar();
					text += e.getKeyChar();
					input = true;
				}*/
				
				if(key == KeyEvent.VK_RIGHT)
				{
					if(index < size.size())
					{
						System.out.println(size.size());
						cursor.moveNext(size.get(index));
						index ++;
					}
				
					System.out.println(">>");
				}
		        else if(key == KeyEvent.VK_LEFT)
				{
					if(index > 0)
					{
						System.out.println(size.size());
					   cursor.movePrev(size.get(index - 1));
					   index --;
					}
					System.out.println(index);
					System.out.println("<<");
				}
		        else if(key == KeyEvent.VK_BACK_SPACE)
		        {
		        	if(index > 0)
		        	{
		        	 cursor.movePrev(size.get(index - 1));
		        	 size.remove(index-1);
		        	 locate.remove(index-1);
		        	 text = text.substring(0, index - 1);
					 index --;
		        	}
		        }
				
			}
			
			
		}
}